package com.stone.front.order.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stone.back.user.entity.BfUser;
import com.stone.base.controller.BaseController;
import com.stone.front.order.entity.BfSite;
import com.stone.front.order.service.ISiteService;
import java.util.Collections;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 联系地址管理
 * @author Stone P
 *
 */
@Controller
@RequestMapping("/siteCtl")
public class SiteController extends BaseController {

	@Autowired
	private ISiteService siteService;
	
	/**
	 * 查询联系地址集合
	 * @param userId	用户Id	
	 * @return
	 */
	@RequestMapping("/selectList")
        public @ResponseBody List<BfSite> selectSiteList(int userId, HttpServletRequest request) {
		List<BfSite> list = this.siteService.selectSiteList(userId);
		return list;
	}
	
	/**
	 * 增加联系地址
	 * @param site	联系地址信息
	 * @return
	 */
	@RequestMapping("/addSite")
        public @ResponseBody BfSite addSite(BfSite site, HttpServletRequest request){
		BfUser user = this.getFrontCurrentUser(request);
		site.setUserid(user.getUserid());
		if(null == site.getSiteid()) {
			this.siteService.addSite(site);
		} else {
			this.siteService.updateSite(site);
		}
		return site;
	}
	
	/**
	 * 删除联系地址
	 * @param siteId	联系地址信息Id
	 * @return
	 */
	@RequestMapping("/deleteSite")
        public @ResponseBody boolean deleteSite(int siteId, HttpServletRequest request){
		this.siteService.deleteSite(siteId);
		return true;
	}
        
        /**
         * 查询当前用户配送地址信息
         * @return 
         */
        @RequestMapping(value = "/api/address", method = RequestMethod.GET)
        public @ResponseBody List<BfSite> queryCurrentUserAddress(HttpServletRequest request) {
            BfUser user = this.getFrontCurrentUser(request);
            if(user == null)
                return Collections.EMPTY_LIST;
            return this.siteService.selectSiteList(user.getUserid());
        }
        
        /**
         * 更新或者保存地址信息
         */
        @RequestMapping(value = "/api/address", method = RequestMethod.POST)
        public HttpEntity<String> updateCurrentUserAddress(@RequestBody BfSite in, HttpServletRequest request) {
            BfUser current = this.getFrontCurrentUser(request);
            if(current == null)
                return new ResponseEntity("未登录", HttpStatus.METHOD_FAILURE);
            
            if(null == in.getSiteid()) {
                in.setUserid(current.getUserid());
                this.siteService.addSite(in);
            }
            else {
                BfSite tmp = siteService.selectSiteDetail(in.getSiteid());
                // 防篡改
                if(!current.getUserid().equals(tmp.getUserid()))
                    throw new UnsupportedOperationException();
                this.siteService.updateSite(in.getSiteid(), in.getSiteinfo(), in.getUsername());
            }
            
            return new ResponseEntity(HttpStatus.OK);
        }
        
        /**
         * 删除用户地址信息
         * 注：使用PUT因为目前angular不支持DELETE
         */
        @RequestMapping(value = "/api/address", method = RequestMethod.PUT)
        public HttpEntity<String> deleteCurrentUserAddress(@RequestBody BfSite in, HttpServletRequest request) {
            BfUser current = this.getFrontCurrentUser(request);
            if(current == null)
                return new ResponseEntity("未登录", HttpStatus.METHOD_FAILURE);
            
            BfSite tmp = siteService.selectSiteDetail(in.getSiteid());
            if(tmp != null) {
                // 非法修改他人信息
                if(!current.getUserid().equals(tmp.getUserid()))
                    throw new UnsupportedOperationException();
                
                this.siteService.deleteSite(in.getSiteid());
            }
            
            return new ResponseEntity(HttpStatus.OK);
        }
        
}
